<?php
/**
 * @package		Joomla.Site
 * @subpackage	com_search
 * @copyright	Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

/**
 *
 * @package		Joomla.Site
 * @subpackage	com_glycophile
 * @since 1.5
 */
class GlycophileModelThemes extends JModelLegacy
{
	/**
	 * data array
	 *
	 * @var array
	 */
	var $_data = null;

	/**
	 * total
	 *
	 * @var integer
	 */
	var $_total = null;

	/**
	 * Pagination object
	 *
	 * @var object
	 */
	var $_pagination = null;

	/**
	 * Constructor
	 *
	 * @since 1.5
	 */
	function __construct()
	{
		parent::__construct();

		//Get configuration
		$app	= JFactory::getApplication();
		$config = JFactory::getConfig();

		// Get the pagination request variables
		$this->setState('limit', $app->getUserStateFromRequest('com_search.limit', 'limit', $config->get('list_limit'), 'uint'));
		$this->setState('limitstart', JRequest::getUInt('limitstart', 0));

		$this->getData();
	}
	
	function getThemeById( $id ){
		$db = $this->getDbo();
		$query = "SELECT * FROM #__glycophile_theme WHERE id=".$id;
		$db->setQuery($query);
		$theme = $db->loadObject();
	}

	/**
	 * Method to get Series list
	 * Si theme s�lectionn�  (exemple Sujet divers id = 16 )
	 *
	 * @access public
	 * @return array
	 */
	function getData()
	{
		$db = $this->getDbo();
		global $mainframe;
		//Get configuration
		$config = JFactory::getConfig();
		// Get the pagination request variables
		
		$limitstart = JRequest::getUInt('limitstart', 1);
		$limit = $config->getValue('config.list_limit');
    $ligne = ($limitstart - 1) * $limit;
// TODO
//		$this->setState('limit', $limit);
//		$this->setState('list.offset', $offset);
		
		$id_theme = JRequest::getVar('theme', 0);
		$query = "SELECT s.*, s.id_theme, COUNT(DISTINCT(ss.id)) AS variante, s.datemodif , th.code, th.theme
		FROM #__glycophile_serie s, #__glycophile_sousserie ss, #__glycophile_tome t, #__glycophile_theme th
		WHERE ss.state = 1 AND s.state = 1 AND t.state = 1";
		$query .= " AND ss.id_serie = s.id"; 
		$query .= " AND s.id_tome = t.id";
		$query .= " AND s.id_theme = th.id";
		$query .= $id_theme != 0 ? " AND s.id_theme = ". $id_theme : '' ;
		$query .= " GROUP BY s.id";
		$query .= " ORDER BY s.dateCreation desc";
		$query .= " Limit ". $ligne .", ". $limit; 
		
		$db->setQuery($query);
		$themes = $db->loadObjectList();
		
		foreach($themes as $theme){
			$query = "Select * From #__glycophile_sousserie ss";
			$query .= "	Where id_serie = ".$theme->id;
			$db->setQuery($query);
			$sseries = $db->loadObjectList();
			$serie->sseries = $sseries;
		}
		
		$this->_data = $themes;
		return $this->_data;
	}




}
